package categoryModel;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

import modules.*;
import parameter.*;
import functions.*;


//Input : WIKI Model
//Output : WIKIIDF Model

public class GenWikiCFICFfromWikiCFMain {

	public static void main(String args[]) throws Exception{
		
		System.out.println("Start");
		
		Map<String, Double> WikiICF = new HashMap<String, Double>();
		FileReader fr_WikiICF = new FileReader(Path.WIKI_ICF_FILEPATH);
		BufferedReader br_WikiICF = new BufferedReader(fr_WikiICF);
		String line = "";
		while ((line = br_WikiICF.readLine()) != null) {
			StringTokenizer token = new StringTokenizer(line, "\t");
			if(token.countTokens() >= 2){
				String token1= token.nextToken();
				String token2= token.nextToken();
				WikiICF.put( token1, Double.parseDouble( token2 ) );
			}
		}			
		br_WikiICF.close();
		fr_WikiICF.close();
		
		List<File> fileListWIKI = FileFunction.getListOfFiles(Path.CATEGORY_MODEL_WIKI_CF_PATH);
		for (File file : fileListWIKI) {
	        Map<String, Double> WikiCFICF = new HashMap<String, Double>();

			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			
			FileWriter fw = new FileWriter(Path.CATEGORY_MODEL_WIKI_CFICF_PATH + file.getName());
			BufferedWriter bw = new BufferedWriter(fw);
			
			line = "";
			while ((line = br.readLine()) != null) {
				StringTokenizer token = new StringTokenizer(line, "\t");
				if(token.countTokens() >= 2){
					String key= token.nextToken();
					String value= token.nextToken();
					if(WikiICF.containsKey(key)){
						WikiCFICF.put(key, Double.parseDouble(value) * WikiICF.get(key) );				
					}else{
						WikiCFICF.put(key, Double.parseDouble(value) * 0.0 );				
					}
				}
			}
			br.close();
			fr.close();
			
			Map<String, Double> WikiCFICF_Norm = TermFunction.getNorm(WikiCFICF);
			
			DoubleValueComparator bvc = new DoubleValueComparator(WikiCFICF_Norm);
			TreeMap<String, Double> tMap = new TreeMap<String, Double>(bvc);
			tMap.putAll(WikiCFICF_Norm);
			
	    	for(Map.Entry<String, Double> entry : tMap.entrySet() ){
	    		bw.write ( entry.getKey() + "\t" + entry.getValue() );
	    		bw.newLine();
	    	}
	    	
	    	bw.close();
	    	fw.close();
			
		}
		
		System.out.println("Complete");

	
		
		
	}

	
}
